;;; init-python.el --- Description -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 gotpl
;;
;; Author: gotpl <631707329@qq.com>
;; Maintainer: gotpl <631707329@qq.com>
;; Created: 2023-08-31 16:26:55
;; Modified: 2024-08-17 00:09:33
;; Package-Requires: ((emacs "24.3"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;;  Description
;;
;;; Code:


(defun gotpl/python-run-current-file ()
  "run the current script"
  (interactive)
  (save-buffer)
  (shell-command
   (concat "python " (shell-quote-argument (buffer-file-name)))))

(with-eval-after-load 'lsp-bridge
  ;; (setq lsp-bridge-python-multi-lsp-server nil)
  (setq lsp-bridge-python-lsp-server 'basedpyright))

;; (setq lsp-bridge-python-lsp-server "pylsp")
;; conda


;; (use-package pyvenv
;;   :ensure (:host github :repo "jorgenschaefer/pyvenv")
;;   :config
;;   (defun try-activate-last ()
;;     "use .dir-locals"
;;     (interactive)
;;     (prin1 "test")
;;     (when (boundp 'my/pyvenv-name)
;;         (pyvenv-workon my/pyvenv-name)))

;;   (let ((work-on-home (getenv "WORKON_HOME")))
;;     (unless work-on-home
;;       (setq work-on-home "~/miniconda3/envs"))
;;     (setenv "WORKON_HOME" (expand-file-name work-on-home)))
  
;;   ;; (add-hook 'python-mode-hook (lambda () (setq-local lsp-bridge-get-single-lang-server-by-project 'local/lsp-bridge-get-single-lang-server-by-project)))


;; ;; (add-hook 'python-ts-mode-hook (lambda () (setq-local lsp-bridge-get-single-lang-server-by-project 'local/lsp-bridge-get-single-lang-server-by-project)))

;;   (add-hook 'pyvenv-post-activate-hooks
;;             (lambda ()
           
;;               (lsp-bridge-restart-process)))
  ;; (defun advice-pyvenv-workon (&rest args)
  ;;   "auto restart lsp-bridge after conda activate"
  ;;   ;; (interactive) ;; 加上会报参数错误
  ;;   (lsp-bridge-restart-process))

  ;; (advice-add 'pyvenv-workon :after 'advice-pyvenv-workon)
  ;; )

;; (if (executable-find "conda")
;;     (use-package conda

;;       :config
;;       ;; if you want interactive shell support, include:
;;       (conda-env-initialize-interactive-shells)
;;       ;; if you want eshell support, include:
;;       (conda-env-initialize-eshell)
;;       (setq-default mode-line-format (cons '(:exec conda-env-current-name) mode-line-format))
;;       ;; (add-hook 'find-file-hook (lambda () (when (bound-and-true-p conda-project-env-path)
;;       ;;                                        (message "activate ... ")
;;       ;;                                        (conda-env-activate-for-buffer))))
;;       (defun advice-conda-env-activate (&optional name)
;;         "auto restart lsp-bridge after conda activate"
;;         (interactive)
;;         (lsp-bridge-restart-process))

;;       (advice-add 'conda-env-activate-path :after 'advice-conda-env-activate)
;;       ;; (defvar conda-env-activate-hook nil "hook for conda activate")
;;       ;; ;; (run-hooks 'conda-env-activate-hook)
;;       ;; (add-hook 'conda-env-activate-hook '(lambda () (lsp-bridge-restart-process)))
;;       ;; (conda-env-autoactivate-mode t) ;; 卡
;;       )
;;   (message "can not find conda command, disable conda."))

;; (with-eval-after-load 'dap-mode
;;   (require 'dap-python)
;;   (setq dap-python-debugger 'debugpy)
;;   )

(use-package conda
  :ensure t
  :commands (conda-env-activate)
  :config
  (conda-mode-line-setup)
  ;; if you want interactive shell support, include:
  (conda-env-initialize-interactive-shells)
  ;; if you want eshell support, include:
  (conda-env-initialize-eshell)
  ;; if you want auto-activation (see below for details), include:
  ;; (conda-env-autoactivate-mode t) ;; 每个文件都会自动，费性能
  (add-hook 'conda-postactivate-hook 'lsp-bridge-restart-process))

;; (use-package realgud-ipdb
;;   :ensure (:files ("*"))
;;   :after realgud
;;   :config
;;   (defun projectile-run-realgud-ipdb ()
;;       "Invoke `gdb' in the project's root."
;;       (interactive)
;;       (projectile-with-default-dir (projectile-acquire-root)
;;           (call-interactively 'realgud:ipdb)))

;;   )

;; (with-eval-after-load 'dape
;;     (add-to-list 'dape-configs
;;              `(debugpy
;;                modes (python-ts-mode python-mode)
;;                command "python3"
;;                command-args ("-m" "debugpy.adapter")
;;                :type "executable"
;;                :request "launch"
;;                :cwd dape-cwd-fn
;;                :program dape-find-file-buffer-default))
;;   )



(provide 'init-python)
;;; init-python.el ends here
